<?php

class Core_Backend_Component_Resource_Controller_Subject extends Dcmp_Component_Controller {

    public function indexAction($pageindex = 1, $pagesize = 20, $sort = 'modifiedtime', $dir = 'desc') {
        $this->getHistory()->record();
        $validator = $this->getRequest()->getValidator('filter');
        $collection = App::getModel('subject/collection_content')
                        ->pagination($pageindex, $pagesize)
                        ->joinAccount()
                        ->sortBy($sort, $dir);
        if ($validator->isNotEmpty('name')) {
            $collection->filterByTitle($validator->getValue('name'));
        }
        if ($validator->isNotEmpty('status')) {
            $collection->filterByStatus($validator->getValue('status'));
        }
        if ($validator->isNotEmpty('top')) {
            $collection->filterByTop($validator->getValue('top'));
        }
        if ($validator->isNotEmpty('new')) {
            $collection->filterByNew($validator->getValue('new'));
        }
        if ($validator->isNotEmpty('hot')) {
            $collection->filterByHot($validator->getValue('hot'));
        }
        if ($validator->isNotEmpty('tag')) {
            $collection->filterByTag($validator->getValue('tag'));
        }
        $collection->load();
        $records = $collection->loadCount();
        return $this->viewResult(array('list' => $collection, 'records' => $records));
    }

    public function roadListAction($subjectid) {
        $this->getHistory()->record();
        $subject = App::getModel('subject/content')->load($subjectid);
        $roads = $subject->getRoads()
                        ->field('g.full_name', 'assigner_name')
                        ->innerJoin('{t}permission_account', 'g', 'g.id', 'f.assigner_id')
                        ->load();
        return $this->viewResult(array('subject' => $subject, 'list' => $roads));
    }

    public function roadAssignAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        return $this->viewResult(array('subject' => $subject));
    }

    public function roadAssignPostAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $assign = $this->getRequest()->getForm('assign');
        $roads = $assign['roads'];
        foreach ($roads as $road) {
            $subject->assignRoad($road,
                    $assign['start_time'],
                    $assign['end_time'],
                    $this->getIdentity()->getId());
        }
        return $this->redirectResult($this->getHistoryUrl('resource/subject/road-list'));
    }

    public function roadEditAction($subjectid, $roadid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $road = $subject->getRoads()
                        ->eq('f.road_id', ':roadId:s')->addParameter('roadId', $roadid)
                        ->load()
                        ->getFirstItem();
        return $this->viewResult(array('subject' => $subject, 'road' => $road));
    }

    public function roadEditPostAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $assign = $this->getRequest()->getForm('assign');
        $subject->assignRoad($assign['road'],
                $assign['start_time'],
                $assign['end_time'],
                $this->getIdentity()->getId());
        return $this->redirectResult($this->getHistoryUrl('resource/subject/road-list'));
    }

    public function roadDeletePostAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $subject->deleteRoad($this->getRequest()->getForm('ids'));
        return $this->redirectResult($this->getHistoryUrl('resource/subject/road-list'));
    }

    public function industryListAction($subjectid) {
        $this->getHistory()->record();
        $subject = App::getModel('subject/content')->load($subjectid);
        $industries = $subject->getIndustries()
                        ->field('g.full_name', 'assigner_name')
                        ->innerJoin('{t}permission_account', 'g', 'g.id', 'f.assigner_id')
                        ->load();
        return $this->viewResult(array('subject' => $subject, 'list' => $industries));
    }

    public function industryAssignAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        return $this->viewResult(array('subject' => $subject));
    }

    public function industryAssignPostAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $assign = $this->getRequest()->getForm('assign');
        $industries = $assign['industries'];
        foreach ($industries as $industry) {
            $subject->assignIndustry($industry,
                    $assign['start_time'],
                    $assign['end_time'],
                    $this->getIdentity()->getId());
        }
        return $this->redirectResult($this->getHistoryUrl('resource/subject/industry-list'));
    }

    public function industryEditAction($subjectid, $industryid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $industry = $subject->getIndustries()
                        ->eq('f.industry_id', ':industryId:s')->addParameter('industryId', $industryid)
                        ->load()
                        ->getFirstItem();
        return $this->viewResult(array('subject' => $subject, 'industry' => $industry));
    }

    public function industryEditPostAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $assign = $this->getRequest()->getForm('assign');
        $subject->assignIndustry($assign['industry'],
                $assign['start_time'],
                $assign['end_time'],
                $this->getIdentity()->getId());
        return $this->redirectResult($this->getHistoryUrl('resource/subject/industry-list'));
    }

    public function industryDeletePostAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $subject->deleteIndustry($this->getRequest()->getForm('ids'));
        return $this->redirectResult($this->getHistoryUrl('resource/subject/industry-list'));
    }

    public function poiListAction($subjectid) {
        $this->getHistory()->record();
        $subject = App::getModel('subject/content')->load($subjectid);
        $pois = $subject->getPois()
                        ->field('g.full_name', 'assigner_name')
                        ->innerJoin('{t}permission_account', 'g', 'g.id', 'f.assigner_id')
                        ->load();
        return $this->viewResult(array('subject' => $subject, 'list' => $pois));
    }

    public function poiAssignAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        return $this->viewResult(array('subject' => $subject));
    }

    public function poiAssignPostAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $assign = $this->getRequest()->getForm('assign');
        $pois = $assign['pois'];
        foreach ($pois as $poi) {
            $subject->assignPoi($poi,
                    $assign['start_time'],
                    $assign['end_time'],
                    $this->getIdentity()->getId());
        }
        return $this->redirectResult($this->getHistoryUrl('resource/subject/poi-list'));
    }

    public function poiEditAction($subjectid, $poiid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $poi = $subject->getPois()
                        ->eq('f.poi_id', ':poiId:s')->addParameter('poiId', $poiid)
                        ->load()
                        ->getFirstItem();
        return $this->viewResult(array('subject' => $subject, 'poi' => $poi));
    }

    public function poiEditPostAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $assign = $this->getRequest()->getForm('assign');
        $subject->assignPoi($assign['poiid'],
                $assign['start_time'],
                $assign['end_time'],
                $this->getIdentity()->getId());
        return $this->redirectResult($this->getHistoryUrl('resource/subject/poi-list'));
    }

    public function poiImportAction($subjectid){
        $subject = App::getModel('subject/content')->load($subjectid);
        return $this->viewResult(array('subject'=>$subject));
    }
    
    public function poiImportPostAction($subjectid){
        $subject = App::getModel('subject/content')->load($subjectid);
        $assign = $this->getRequest()->getForm('assign');
        Kd_Util_IO::makeDirectory(DP_TMP.'import'.DS.'resources');
        $file = $this->getRequest()->getFile('upload_file')->saveRandom(DP_TMP.'import'.DS.'resources');
        $pois = explode("\r\n", file_get_contents($file));
        foreach($pois as $poiId){
	        $subject->assignPoi($poiId,
	                $assign['start_time'],
	                $assign['end_time'],
	                $this->getIdentity()->getId());        
        }
        return $this->redirectResult($this->getHistoryUrl('resource/subject/poi-list'));
    }    

    public function poiDeletePostAction($subjectid) {
        $subject = App::getModel('subject/content')->load($subjectid);
        $subject->deletePoi($this->getRequest()->getForm('ids'));
        return $this->redirectResult($this->getHistoryUrl('resource/subject/poi-list'));
    }

    public function assignAccountPostAction() {
        $pois = App::getModel('foundation/collection_poi')
                        ->contain('a.id', ':poiIds:s')
                        ->addParameter('poiIds', $this->getRequest()->getForm('ids'))
                        ->load();
        $accounts = App::getModel('permission/collection_account')->load();
        $selectedAccounts = Kd_Data_Factory::getQueryDcl()
                        ->from('{t}foundation_poi_account')
                        ->contain('poi_id', ':poiIds:s')
                        ->addParameter('poiIds', $this->getRequest()->getForm('ids'))
                        ->field('account_id')
                        ->distinct()
                        ->fetch()
                        ->getFieldValues('account_id');
        return $this->viewResult(array('accounts' => $accounts, 'account_ids' => $selectedAccounts, 'pois' => $pois));
    }

    public function saveAssignPostAction() {
        $poiIds = $this->getRequest()->getForm('poi_ids');
        $accountIds = $this->getRequest()->getForm('account_ids');
        Kd_Data_Factory::getDeleteDcl()
                ->from('{t}foundation_poi_account')
                ->where()->contain('poi_id', ':poiIds:s')->closest()
                ->addParameter('poiIds', $poiIds)
                ->execute();

        $sql = Kd_Data_Factory::getInsertDcl()
                        ->into('{t}foundation_poi_account')
                        ->value('poi_id', ':poiId:s')
                        ->value('account_id', ':accountId:i')
                        ->toSql();
        $driver = Kd_Db_Factory::getDriver();
        foreach ($accountIds as $accountId) {
            foreach ($poiIds as $poiId) {
                $driver->setSql($sql, array('poiId' => $poiId, 'accountId' => $accountId))->execute();
            }
        }
        return $this->redirectResult($this->getHistoryUrl('foundation/poi/index'));
    }

}